package com.cloud.wms.injection.modules;

import android.content.Context;
import android.content.res.Resources;

import com.cloud.core.tools.ActivityManager;
import com.cloud.core.tools.StorageManager;
import com.cloud.core.tools.ToastManager;
import com.cloud.printer.FineExPrinter;
import com.cloud.wms.BuildConfig;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.config.HttpConfig;
import com.cloud.wms.http.HttpAPI;
import com.cloud.wms.http.interceptor.BaseUrlInterceptor;
import com.cloud.wms.http.interceptor.DownLoadInterceptor;
import com.cloud.wms.http.interceptor.RequestInterceptor;
import com.cloud.wms.http.url.SiteManager;
import com.cloud.wms.injection.qualifiers.ForApplication;
import com.cloud.wms.tools.CrashHandler;
import com.cloud.wms.ui.main.entity.LoginCache;
import com.tencent.mmkv.MMKV;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Tanghaibo
 * 2018/3/21
 * description: Application Module
 */

@Module
public final class ApplicationModule {
    private final FineExApplication mApplication;

    public ApplicationModule(FineExApplication application) {
        this.mApplication = application;
    }

    @ForApplication
    @Singleton
    @Provides
    public Context provideApplicationContext() {
        return mApplication;
    }

    @Singleton
    @Provides
    public Resources provideResources() {
        return mApplication.getResources();
    }

    @Singleton
    @Provides
    public ActivityManager provideActivityManager(){
        ActivityManager.init(mApplication);
        return ActivityManager.getInstance();
    }

    @Singleton
    @Provides
    public ToastManager provideToastManager() {
        return new ToastManager(mApplication);
    }

    @Singleton
    @Provides
    public StorageManager provideStorageManager() {
        return new StorageManager(mApplication);
    }

    @Singleton
    @Provides
    public MMKV provideKV() {
        return MMKV.defaultMMKV();
    }

    @Singleton
    @Provides
    public CrashHandler provideCrashHandler(StorageManager storageManager) {
        return new CrashHandler(mApplication, storageManager);
    }

    @Singleton
    @Provides
    public RequestInterceptor provideRequestInterceptor() {
        return new RequestInterceptor();
    }

    @Singleton
    @Provides
    public BaseUrlInterceptor provideBaseUrlInterceptor() {
        return new BaseUrlInterceptor();
    }

    @Singleton
    @Provides
    public DownLoadInterceptor provideDownLoadInterceptor() {
        return new DownLoadInterceptor();
    }

    @Singleton
    @Provides
    public OkHttpClient provideOkHttpClient(BaseUrlInterceptor baseUrlInterceptor, RequestInterceptor requestInterceptor, DownLoadInterceptor downLoadInterceptor) {
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //debug环境下打印网络请求日志
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addNetworkInterceptor(httpLoggingInterceptor);
        }
        builder.connectTimeout(HttpConfig.CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(HttpConfig.IO_READ_TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(HttpConfig.IO_WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                .addNetworkInterceptor(downLoadInterceptor)
                .addInterceptor(baseUrlInterceptor)
                .addInterceptor(requestInterceptor);
        return builder.build();
    }

    @Singleton
    @Provides
    public Retrofit provideRetrofit(OkHttpClient okHttpClient) {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.client(okHttpClient)
                .baseUrl(SiteManager.getBaseUrl())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create());
        return builder.build();
    }

    @Singleton
    @Provides
    public HttpAPI provideHttpAPI(Retrofit restAdapter) {
        return restAdapter.create(HttpAPI.class);
    }

    @Singleton
    @Provides
    public LoginCache provideLoginCache() {
        return LoginCache.getInstance();
    }

    @Singleton
    @Provides
    public FineExPrinter providePrinter() {
        return new FineExPrinter();
    }
}
